﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mgh.Data.Sql;
 using A2Z.DataAccess;

namespace A2Z.Bussiness
{
    public class TeamBLL : A2Z.DataAccess.TeamDA
    {
        public TeamBLL(DataConnection cnn)
        {
            this.IConnection = cnn;
        }

        #region get

        public dataSource.Fb_Vw_TeamDataTable getFTeamsByTour(string tourID, string groupID)
        {
            if (string.IsNullOrEmpty(tourID))
            {
                addMessage("", "tourID is null/empty", 1);
                return null;
            }
            if (string.IsNullOrEmpty(groupID))
            {
                addMessage("", "tourID is null/empty", 1);
                return null;
            }
            return base._getFTeamsByTour(tourID, groupID);
        }

        public dataSource.Fb_Tbl_TeamDataTable getTeamByID(string teamID)
        {
            if (string.IsNullOrEmpty(teamID))
            {
                addMessage("", "teamID is null/empty", 1);
                return null;
            }
            List<string> listID = new List<string>();
            listID.Add(teamID);
            return base._getTeamsByID(listID);
        }

        #endregion

        #region insert
        
        public bool insertTeam(dataSource.Fb_Tbl_TeamRow row)
        {
            if (row == null)
            {
                addMessage("", "inserted data is null", 1);
                return false;
            }
            dataSource.Fb_Tbl_TeamDataTable tb = new dataSource.Fb_Tbl_TeamDataTable();
            tb.Rows.Add(row.ItemArray);
            return base._insertTeams(tb);
        }

        #endregion

        #region update

        public bool udpateTeam(dataSource.Fb_Tbl_TeamRow row)
        {
            if (row == null)
            {
                addMessage("", "updated data is null", 1);
                return false;
            }
            dataSource.Fb_Tbl_TeamDataTable tb = new dataSource.Fb_Tbl_TeamDataTable();
            tb.Rows.Add(row.ItemArray);
            return base._updateTeams(tb);
        }

        #endregion

        #region delete
        #endregion

        #region other

        public bool clockCTeam(string cteamID)
        {
            if (string.IsNullOrEmpty(cteamID))
            {
                addMessage("", "cteamID is null/empty", 1);
                return false;
            }
            dataSource.Fb_Tbl_Current_TeamDataTable tb = new dataSource.Fb_Tbl_Current_TeamDataTable();
            dataSource.Fb_Tbl_Current_TeamRow row = tb.NewFb_Tbl_Current_TeamRow();
            row.CTeamID = cteamID;
            row.Clock = true;
            tb.AddFb_Tbl_Current_TeamRow(row);
            return base._updateCTeamsWithColumn(tb, tb.ClockColumn.ColumnName);
        }

        public bool unclockCTeam(string cteamID)
        {
            if (string.IsNullOrEmpty(cteamID))
            {
                addMessage("", "cteamID is null/empty", 1);
                return false;
            }
            dataSource.Fb_Tbl_Current_TeamDataTable tb = new dataSource.Fb_Tbl_Current_TeamDataTable();
            dataSource.Fb_Tbl_Current_TeamRow row = tb.NewFb_Tbl_Current_TeamRow();
            row.CTeamID = cteamID;
            row.Clock = false;
            tb.AddFb_Tbl_Current_TeamRow(row);
            return base._updateCTeamsWithColumn(tb, tb.ClockColumn.ColumnName);
        }

        public bool check2CTeamSameGroup(string cteamID1, string cteamID2)
        {
            if (string.IsNullOrEmpty(cteamID1))
            {
                addMessage("", "cteamID1 is null/empty", 1);
                return false;
            }

            if (string.IsNullOrEmpty(cteamID2))
            {
                addMessage("", "cteamID2 is null/empty", 1);
                return false;
            }
            List<string> listID = new List<string>();
            listID.Add(cteamID1);
            listID.Add(cteamID2);
            dataSource.Fb_Vw_TeamDataTable tb = base._getFTeamsByID(listID);
            if (tb == null)
                return false;
            if (tb.Count <= 1)
            {
                addMessage("", "One more team is not exist", 1);
                return false;
            }
            string gr0 = tb[0].IsGroupIDNull() ? string.Empty : tb[0].GroupID;
            string gr1 = tb[1].IsGroupIDNull() ? string.Empty : tb[1].GroupID;
            return gr0.Equals(gr1);
        }

        #endregion
    }
}
